<?php
/**
 * Created by lzl
 * Date: 2021 2021/9/14
 * Time: 13:36
 */
namespace App\Forms;


use App\Models\Order;
use App\Models\User;
use App\Models\UserAddress;
use App\Models\UserBalanceLog;
use App\Models\UserShareCode;
use Illuminate\Database\Query\Builder;

class UserForms extends BaseForm
{

    /**
     * 会员列表
     * @param $validated
     * @return mixed
     */
    public function getUserList($validated)
    {
        $list = User::where(function ($q) use ($validated) {
            /**
             * @var Builder $q
             */
            if (isset($validated['name'])) {
                $q->where('nick_name', 'like', "%{$validated['name']}%")
                    ->orWhere('mobile', 'like', "%{$validated['name']}%");
            }

            if (isset($validated['level'])) {
                $q->where('level', $validated['level']);
            }

            if (isset($validated['pid'])) {
                $q->where('pid', $validated['pid']);
            }

            $q->whereNotNull('nick_name');

            return $q;

        })->page($validated['page'], $validated['limit'])
            ->orderBy('id', 'desc')
            ->get();

        return $list;
    }


    /**
     * 用户详情
     * @param $uid
     * @param array $field
     * @return User|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getUserDetail($uid, $field = ['*'])
    {
        return User::whereKey($uid)->first($field);
    }


    /**
     * @param $uid
     * @return int
     */
    public function getChildrenCount($uid)
    {
        return User::where('pid', $uid)->count();
    }

    /**
     * @param $uid
     * @param $validated
     * @return bool|int
     */
    public function editMsg($uid, $validated)
    {
        return User::whereKey($uid)->update($validated);
    }



    /**
     * @param $uid
     */
    public function deleteUserShareCode($where)
    {
        UserShareCode::where($where)->delete();
    }


    public function createUserShareCode($param)
    {
        return UserShareCode::create($param);
    }


    public function getUserShareCode($where)
    {
        return UserShareCode::where($where)->get();
    }

    /**
     * @param $where
     * @return UserShareCode|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getUserShareSingle($where)
    {
        return UserShareCode::where($where)->first();
    }

    /**
     * @param $param
     * @return UserBalanceLog|\Illuminate\Database\Eloquent\Model
     */
    public function balanceLogCreate($param)
    {
        return UserBalanceLog::create($param);
    }


    /**
     * @param $validated
     * @return mixed
     */
    public function getUserBalanceLog($validated)
    {
        $list = UserBalanceLog::where(function ($q) use ($validated) {
            /**
             * @var Builder $q
             */

            if (isset($validated['uid'])) {
                $q->where('uid', $validated['uid']);
            }

            return $q;

        })->page($validated['page'], $validated['limit'])
            ->orderBy('id', 'desc')
            ->get();

        return $list;
    }


    /**
     * @return array
     */
    public function getUserBalanceCount()
    {
        $today = number_format(UserBalanceLog::whereBetween('create_at', [date('Y-m-d 00:00:00'), date('Y-m-d 23:59:59')])
            ->where('uid', UID)
            ->where('type',UserBalanceLog::COMMISSION_IN)
            ->sum('amount'),2);

        $month = number_format(UserBalanceLog::whereBetween('create_at', [date('Y-m-01 00:00:00'), date('Y-m-d H:i:s', time())])
            ->where('uid', UID)
            ->where('type',UserBalanceLog::COMMISSION_IN)
            ->sum('amount'),2);

        return compact('today', 'month');
    }


    /**
     * @return array
     */
    public function getUserOrderCount()
    {
        $today = Order::where('pid', UID)
            ->where('status', Order::SUCCESS)
            ->whereBetween('create_at', [date('Y-m-d 00:00:00'), date('Y-m-d 23:59:59', time())])
            ->count();
        $month = Order::where('pid', UID)
            ->where('status', Order::SUCCESS)
            ->whereBetween('create_at', [date('Y-m-01 00:00:00'), date('Y-m-d H:i:s', time())])
            ->count();
        $total = Order::where('pid', UID)
            ->where('status', Order::SUCCESS)
            ->count();

        return compact('today','month', 'total');
    }
}